import $ from 'jquery'
import {getUrlParam} from 'as-common'
import BuyAPI from '../repo/api/BuyAPI'
import {Toast} from 'vant'
import router from '../router'
import EnterpriseLevelAPI from '../repo/api/EnterpriseLevelAPI'
import {generateRouterPath} from "../router/util";

let payTool = {
  //移动端 PC端调起支付宝支付
  invokeALI: function (data) {
    let objForm = document.createElement('form')
    objForm.name = 'alipay_form'
    objForm.method = 'post'
    let objInput = document.createElement('input')
    objInput.type = 'hidden'
    objInput.name = 'biz_content'
    objForm.append(objInput)
    $('body').append(objForm)
    let reqData = data.requestData
    let txt = ''
    let params = getUrlParam('?' + reqData)//将url参数转成json

    let obj = reqData.split('&')
    obj = obj.filter(function (el) {//去除java返回参数里面的biz_content  这个参数需要单独post提交
      return el.indexOf('biz_content') < 0
    })
    txt = obj.join('&')
    let form = document.forms['alipay_form']
    form.action = 'https://openapi.alipay.com/gateway.do?' + txt
    form['biz_content'].value = decodeURIComponent(params.biz_content)
    form.submit()
  },
  aliAddOrderAndInvokePay(params) {
    let param = {
      targetId: params.targetId,
      type: 1,
      targetType: params.targetType,
      payType: 1,  //payType：1微信，3支付宝。
      applyType: 34,  //applyType 区别与app端调起的支付宝支付,
      returnUrl: params.returnUrl
    }

    BuyAPI.addOrder(param).then((result) => {
      let data = result.responseData
      let state = data.status
      //免费购买成功的不用去调用微信下单接口。
      if (state === 2) {
        Toast('购买成功')
        setTimeout(() => {
          router.push('/')
        }, 500)
        return
      }
      if (data) {
        let jsons = JSON.parse(data.requestData)
        let webUrl = jsons.mweb_url

        let path = generateRouterPath('/web-hft-ee/purchase/result/0/0/0')
        let url = window.location.origin + path

        let prepayId = jsons.package
        prepayId = prepayId.replace('prepay_id=', '')

        window.location.href = webUrl + '&redirect_url=' + url + '?prepayId=' + prepayId
      }
    })
  },
  aliPayAddOrderOfEvaluationInvoke(params) {
    let sendData = {
      targetType: params.targetId,
      payType: 1,  //payType：1微信，3支付宝。
      type: 1,
      applyType: 34,  //applyType 区别与app端调起的支付宝支付,
      month: params.months || 3,
      returnUrl: params.returnUrl
    }
    EnterpriseLevelAPI.addOrder(sendData).then((result) => {
      let data = result.responseData
      let state = data.status
      if (state === 2) {  //免费购买成功的不用去调用微信下单接口。
        Toast('购买成功')
        setTimeout(() => {
          router.push('/')
        }, 500)
        return
      }

      if (data) {

        let jsons = JSON.parse(data.requestData)
        let webUrl = jsons.mweb_url

        let path = generateRouterPath('/web-hft-ee/purchase/result/0/0/0')
        let url = window.location.origin + path

        let prepayId = jsons.package
        prepayId = prepayId.replace('prepay_id=', '')

        window.location.href = webUrl + '&redirect_url=' + url + '?prepayId=' + prepayId
      }

    })
  }
}

export {payTool}
